#include <stdint.h>


int16_t wave_modify_loop_104(int16_t o2per, int16_t altitude) {
    static uint8_t hsta = 0;
    static int16_t last_off = 0;
    const int16_t hysteresis = 100;
    uint8_t out_sta = 0;
 
    /* 查表找到最佳、上限值 */
    cbset.gears = _find_curve_best(altitude, &cbset.downlimit, &cbset.best, &
cbset.uplimit);
    cbset.best_off = o2per - cbset.best;

    if( cbset.best_off > hysteresis && last_off > hysteresis ) {
        hsta = 0;
    }else if( cbset.best_off < -hysteresis && last_off < -hysteresis ){
        hsta = 1;
    }
    
    if( 0 == hsta ) {
        o2per+=400;
        out_sta = 4;
    }else if( 1 == hsta && o2per > 2300 ){
        o2per-=300;
        out_sta = 5;
    }

    last_off = cbset.best_off;
    _sta_update(out_sta);
	printf("o = %d\r\n",o2per );
    return o2per;
}

int main(void) {
	int16_t raw = 2000 , int16_t a = 1500;
	wave_modify_loop_104(raw, 1500);
	wave_modify_loop_104(raw+100, 1500);
	wave_modify_loop_104(raw+300, 1500);
	
	
	
}
